const CacheManager = require("../template")

function testCacheManagerPerformance() {
  console.log(
    "1. 大量缓存设置性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      const end = performance.now()
      const setTime = end - start
      
      return setTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "2. 大量缓存获取性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 设置缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 获取缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.get(`key${i}`)
      }
      
      const end = performance.now()
      const getTime = end - start
      
      return getTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "3. 大量缓存删除性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 设置缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 删除缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.delete(`key${i}`)
      }
      
      const end = performance.now()
      const deleteTime = end - start
      
      return deleteTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "4. 大量缓存存在检查性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 设置缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 检查存在
      for (let i = 0; i < cacheCount; i++) {
        cache.has(`key${i}`)
      }
      
      const end = performance.now()
      const hasTime = end - start
      
      return hasTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "5. 混合操作性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const operationCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < operationCount; i++) {
        const operation = Math.floor(Math.random() * 4)
        
        if (operation === 0) {
          cache.set(`key${i}`, `value${i}`)
        } else if (operation === 1) {
          cache.get(`key${i}`)
        } else if (operation === 2) {
          cache.has(`key${i}`)
        } else {
          cache.delete(`key${i}`)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "6. 缓存管理器内存效率测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 添加大量缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      // 验证内存使用合理
      const size = cache.size()
      const keys = cache.keys()
      
      return size === cacheCount &&
             keys.length === cacheCount
    })()
  )

  console.log(
    "7. 缓存管理器并发操作性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const operationCount = 1000
      
      const start = performance.now()
      
      // 并发操作
      for (let i = 0; i < operationCount; i++) {
        cache.set(`key${i}`, `value${i}`)
        cache.get(`key${i}`)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "8. 缓存管理器批量操作性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const batchSize = 100
      const batches = 10
      
      const start = performance.now()
      
      // 批量操作
      for (let batch = 0; batch < batches; batch++) {
        for (let i = 0; i < batchSize; i++) {
          cache.set(`batch${batch}_key${i}`, `batch${batch}_value${i}`)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "9. 缓存管理器清理性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 添加缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`, 50) // 50ms过期
      }
      
      const start = performance.now()
      
      // 等待过期后清理
      setTimeout(() => {
        const cleaned = cache.cleanup()
        return cleaned > 0
      }, 75)
      
      const end = performance.now()
      const cleanupTime = end - start
      
      return cleanupTime < 1000
    })()
  )

  console.log(
    "10. 缓存管理器统计性能测试:",
    (() => {
      const cache = new CacheManager(1000)
      const cacheCount = 1000
      
      // 添加缓存
      for (let i = 0; i < cacheCount; i++) {
        cache.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 获取统计信息
      const stats = cache.getStats()
      
      const end = performance.now()
      const statsTime = end - start
      
      return stats.sets === cacheCount &&
             statsTime < 100
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(count) {
          const cache = new CacheManager()
          const start = performance.now()
          
          for (let i = 0; i < count; i++) {
            cache.set(`key${i}`, i)
          }
          
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: count * this.multiplier
          }
        }
      }
      
      const result = perfObj.processPerformance(100)
      
      return result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 缓存管理器性能一致性测试:",
    (() => {
      const cache1 = new CacheManager(100)
      const cache2 = new CacheManager(100)
      const cacheCount = 100
      
      // 相同操作测试一致性
      const times1 = []
      const times2 = []
      
      for (let i = 0; i < 10; i++) {
        const start1 = performance.now()
        for (let j = 0; j < cacheCount; j++) {
          cache1.set(`key${j}`, j)
        }
        const end1 = performance.now()
        times1.push(end1 - start1)
        
        const start2 = performance.now()
        for (let j = 0; j < cacheCount; j++) {
          cache2.set(`key${j}`, j)
        }
        const end2 = performance.now()
        times2.push(end2 - start2)
      }
      
      // 验证性能一致性
      const avgTime1 = times1.reduce((a, b) => a + b, 0) / times1.length
      const avgTime2 = times2.reduce((a, b) => a + b, 0) / times2.length
      
      return avgTime1 < 100 &&
             avgTime2 < 100 &&
             Math.abs(avgTime1 - avgTime2) < 50
    })()
  )
}

testCacheManagerPerformance()
